﻿using Adnc.Shared.ConfigModels;
using AutoMapper.Configuration;
using Microsoft.AspNetCore.Http;
using SKIT.FlurlHttpClient;
using SKIT.FlurlHttpClient.Wechat.Api;
using System;
using System.Collections.Generic;
using System.Text;

namespace Adnc.Application.Shared.WeChat
{
    public class WeChatApiHttpClientFactory
    {
        /// <summary>
        /// 获取公众号客户端
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static WechatApiClient CreateWeXinClient(WeChatOption config)
        {
            if (string.IsNullOrEmpty(config.WeiXinAppId) || string.IsNullOrEmpty(config.WeiXinAppSecret))
                throw new Exception("未在配置项中找到微信公众号配置讯息。");

            var wechatApiClient = new WechatApiClient(new WechatApiClientOptions()
            {
                AppId = config.WeiXinAppId,
                AppSecret = config.WeiXinAppSecret,
            });

            wechatApiClient.Configure(settings =>
            {
                settings.JsonSerializer = new FlurlNewtonsoftJsonSerializer();
            });

            return wechatApiClient;
        }

        /// <summary>
        /// 获取小程序客户端
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static WechatApiClient CreateWxOpenClient(WeChatOption config)
        {
            if (string.IsNullOrEmpty(config.WxOpenAppId) || string.IsNullOrEmpty(config.WxOpenAppSecret))
                throw new Exception("未在配置项中找到微信小程序配置讯息。");

            var WechatApiClient = new WechatApiClient(new WechatApiClientOptions()
            {
                AppId = config.WxOpenAppId,
                AppSecret = config.WxOpenAppSecret
            });

            WechatApiClient.Configure(settings =>
            {
                settings.JsonSerializer = new FlurlNewtonsoftJsonSerializer();
            });

            return WechatApiClient;
        }
    }
}
